package com.starrin.tennisfinder;

import com.starrin.tennisfinder.model.BookingStatus;
import com.starrin.tennisfinder.model.VenueAvailabilityDataHolder;
import com.starrin.tennisfinder.system.Cache;
import org.apache.log4j.Logger;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * User: Starrin
 * Date: 2013-10-26
 * Time: 13:21
 */
@ManagedBean
@ViewScoped
public class TennisFinderBean implements Serializable {

    private Date date;

    private List<VenueAvailabilityDataHolder> venueAvailabilityList;

    private List<String> venueList = new ArrayList<>();

    private boolean previousDateButtonDisabled;
    private static final Logger logger = Logger.getLogger(TennisFinderBean.class);

    @PostConstruct
    public void init() {
        date = new Date();
        venueAvailabilityList = refreshVenueAvailabilityList();
    }

    public void refreshData() {
       if(date!= null) {
            venueAvailabilityList = refreshVenueAvailabilityList();
       }
    }

    private List<VenueAvailabilityDataHolder> refreshVenueAvailabilityList() {
        List<VenueAvailabilityDataHolder> venueAvailabilityDataHolderList = new ArrayList<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String dateString = df.format(date);

        try {
            Cache cache = Cache.getInstance();

            venueAvailabilityDataHolderList.add(cache.getEntry(dateString, "hellas"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"karrtorp"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"solna"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"stockholmtennis"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"salk"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"tennisstadion"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"malmen"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"sumpan"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"farsta"));
            venueAvailabilityDataHolderList.add(cache.getEntry(dateString,"tyreso"));

            refreshIsPreviousActive();
            pupulateVenueList(venueAvailabilityDataHolderList);

        } catch (Exception e) {
            logger.error("Error fetching data, cause", e);
        }

        return venueAvailabilityDataHolderList;
    }

    private void pupulateVenueList(List<VenueAvailabilityDataHolder> venueAvailabilityDataHolderList) {
        for (VenueAvailabilityDataHolder venueAvailabilityDataHolder : venueAvailabilityDataHolderList) {
            venueList.add(venueAvailabilityDataHolder.getVenueName());
        }
    }

    public void nextDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        date = cal.getTime();
        venueAvailabilityList = refreshVenueAvailabilityList();
    }

    public void previousDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        date = cal.getTime();
        venueAvailabilityList = refreshVenueAvailabilityList();
    }

    public void refreshIsPreviousActive() {
        Date today = new Date ();
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTime(today);

        Calendar selectedDate = new GregorianCalendar();
        selectedDate.setTime(date);

        previousDateButtonDisabled = selectedDate.get(Calendar.DAY_OF_YEAR) <= currentDate.get(Calendar.DAY_OF_YEAR);
    }

    public String getStatusColor(BookingStatus bookingStatus) {
        if(bookingStatus.equals(BookingStatus.INDOORS) || bookingStatus.equals(BookingStatus.OUTDOORS) || bookingStatus.equals(BookingStatus.INDOORS_OUTDOORS)) {
            return "#CCFF99";

        } else if (bookingStatus.equals(BookingStatus.BOOKED)) {
            return "#DB4D4D";
        }
        else return "E9E9E9";
    }

    public boolean getBookable(BookingStatus bookingStatus) {
        return bookingStatus.equals(BookingStatus.INDOORS_OUTDOORS) || bookingStatus.equals(BookingStatus.INDOORS) || bookingStatus.equals(BookingStatus.OUTDOORS);
    }

    public String getBookingText(BookingStatus bookingStatus) {
        String bookingText = "";
        if (bookingStatus.equals(BookingStatus.INDOORS_OUTDOORS)) {
            bookingText +=   "<i class=\"fa fa-home\"/></i> <i class=\"fa fa-sun-o\"/></i>";
        } else if(bookingStatus.equals(BookingStatus.INDOORS)) {
            bookingText +=   "<i class=\"fa fa-home\"/></i> <span style=\"color:lavender;\"><i class=\"fa fa-sun-o\"/></i></span>";
        } else if(bookingStatus.equals(BookingStatus.OUTDOORS)) {
            bookingText +=   "<span style=\"color:lavender;\"><i class=\"fa fa-home\"/></i></span> <i class=\"fa fa-sun-o\"/></i>";

        }
        return bookingText;
    }

    public String getBookingLinkTitle(BookingStatus bookingStatus) {
        String bookingText = "";
        if (bookingStatus.equals(BookingStatus.INDOORS_OUTDOORS)) {
            bookingText +=   "Inomhus och utomhus";
        } else if(bookingStatus.equals(BookingStatus.INDOORS)) {
            bookingText +=   "Endast inomhus";
        } else if(bookingStatus.equals(BookingStatus.OUTDOORS)) {
            bookingText +=   "Endast utomhus";
        }
        return bookingText;
    }

    public Locale getLocale() {
        return new Locale("sv","SE");
    }

    public String getCurrentDay() {
        SimpleDateFormat df = new SimpleDateFormat("EEEE", new Locale("sv", "SE"));
        String dateStr = df.format(date);
        return dateStr.substring(0, 1).toUpperCase() + dateStr.substring(1);
    }


    public List<String> getVenueList() {
        return venueList;
    }

    public void setVenueList(List<String> venueList) {
        this.venueList = venueList;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setVenueAvailabilityList(List<VenueAvailabilityDataHolder> venueAvailabilityList) {
        this.venueAvailabilityList = venueAvailabilityList;
    }

    public List<VenueAvailabilityDataHolder> getVenueAvailabilityList() {
        return venueAvailabilityList;
    }

    public boolean isPreviousDateButtonDisabled() {
        return previousDateButtonDisabled;
    }

}
